import 'package:bxultimate/app/pages/application/application_index/widgets/my_org.dart';
import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/indicators/material_indicator.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_search_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';
import 'widgets/apply_record.dart';

class ApplicationIndexPage extends StatefulWidget {
  const ApplicationIndexPage({Key? key}) : super(key: key);

  @override
  State<ApplicationIndexPage> createState() => _ApplicationIndexPageState();
}

class _ApplicationIndexPageState extends State<ApplicationIndexPage> {
  final controller = Get.find<ApplicationIndexController>();
  final state = Get.find<ApplicationIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: SafeArea(
        child: _buildBody(),
      ),
    );
  }

  _buildModule() {
    var moduleList = [
      // {
      //   "title": "组织管理",
      //   "icon": Assets.applicationZzgl,
      //   "onTap": () {
      //     print("1");
      //   }
      // },
      {
        "title": "役前教育",
        "icon": Assets.applicationYqjy,
        "onTap": () {
          Get.toNamed(RouterName.preServiceEducationIndex);
        }
      },
      {
        "title": "就业创业",
        "icon": Assets.applicationJycy,
        "onTap": () {
          Get.toNamed(RouterName.applicationNewsIndex,
              arguments: {"title": "就业创业", "id": 199});
        }
      },
      {
        "title": "应急知识",
        "icon": Assets.applicationYjzs,
        "onTap": () {
          Get.toNamed(RouterName.applicationNewsIndex,
              arguments: {"title": "应急知识", "id": 200});
        }
      },
      {
        "title": "学习教育",
        "icon": Assets.applicationZzgl,
        "onTap": () {
          String url =
              'https://sxmbxt.com/cms/chanzhieps_edu_api/web/root.html?uid=1720265592295837764&regionId=140109&name=%E9%83%AD%20%E5%B0%8F%E8%B4%A4&img=undefined';
          Get.toNamed(RouterName.mkWebView,
              arguments: {"title": "学习教育", "url": url});
        }
      }
    ];

    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Wrap(
        alignment: WrapAlignment.start,
        children: [
          for (int i = 0; i < moduleList.length; i++)
            _buildModuleItem(moduleList[i]['icon'], moduleList[i]['title'],
                moduleList[i]['onTap']),
        ],
      ),
    );
  }

  _buildModuleItem(icon, name, onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: (Get.width - AppTheme.iDefaultPadding * 2) / 4,
          padding: EdgeInsets.all(AppTheme.iDefaultPadding),
          child: Column(children: [
            MkLoadImage(
              icon,
              width: 40,
              height: 40,
            ),
            Gaps.vGap8,
            Text(
              name,
              style: TextStyle(fontSize: 12),
            )
          ])),
    );
  }

  _buildRecommandOrganization() {
    return Column(
      children: [
        MkSearchBar(
          onTap: null,
          showBack: false,
          editingController: controller.conditionController,
          onPressed: (context) {
            controller.getData();
          },
        ),
        Gaps.vGap8,
        ...state.recommandList.map((e) => Container(
              padding: EdgeInsets.all(AppTheme.iDefaultPadding),
              margin: EdgeInsets.only(bottom: AppTheme.iDefaultPadding),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
                  image: DecorationImage(
                      image: AssetImage(Assets.applicationOrgBg),
                      fit: BoxFit.cover)),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('${e.name}',
                      style:
                          TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
                  Gaps.vGap8,
                  Text(
                    '${e.address}',
                    style: TextStyle(color: Color(0xFF606266)),
                  ),
                  Gaps.vGap8,
                  Row(
                    children: [
                      e.typeName == ''
                          ? SizedBox()
                          : TDTag(
                              '${e.typeName}',
                              theme: TDTagTheme.warning,
                              isOutline: true,
                            ),
                      Gaps.hGap8,
                      e.teamLevelName == ''
                          ? SizedBox()
                          : TDTag(
                              '${e.teamLevelName}',
                              theme: TDTagTheme.warning,
                              isOutline: true,
                            ),
                    ],
                  )
                ],
              ),
            ))
      ],
    );
  }

  _buildOrg() {
    return Column(
      children: [
        Container(
          alignment: Alignment.topLeft,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              TabBar(
                isScrollable: true,
                indicator: MaterialIndicator(color: Color(0xffFF8362)),
                indicatorColor: Color(0xFFFF8362),
                unselectedLabelColor: Color(0xFF606266),
                labelColor: Colors.black,
                unselectedLabelStyle: TextStyle(fontSize: 14),
                labelStyle:
                    TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                tabs:
                    controller.tabItem.map((e) => Tab(child: Text(e))).toList(),
                controller: controller.tabController,
              ),
              GestureDetector(
                onTap: controller.toOrganizationCreate,
                child: MkLoadImage(
                  Assets.applicationAdd,
                  width: 30,
                ),
              )
            ],
          ),
        ),
        Gaps.vGap8,
        Expanded(
            child: TabBarView(
          controller: controller.tabController,
          children: [
            MyOrg(),
            ApplyRecord(),
            SingleChildScrollView(
              child: _buildRecommandOrganization(),
            ),
          ],
        ))
      ],
    );
  }

  Widget _buildNoLogin() {
    return Center(
      child: TDEmpty(
        type: TDEmptyType.plain,
        emptyText: '登录后查看组织信息',
        image: Container(
          width: 200,
          height: 200,
          decoration: BoxDecoration(
              borderRadius:
                  BorderRadius.circular(TDTheme.of(context).radiusDefault),
              image:
                  const DecorationImage(image: AssetImage(Assets.imagesNoOrg))),
        ),
      ),
    );
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // AppTitle(
          //   title: '功能速递',
          //   icon: Assets.applicationGnsd,
          // ),
          // Gaps.vGap8,
          // _buildModule(),
          Gaps.vGap8,
          AuthStore.to.isLogin
              ? Expanded(child: _buildOrg())
              : Expanded(child: _buildNoLogin())
        ],
      ),
    );
  }
}
